package com.yuntongxun.ecdemo.voicechat.ui.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import java.io.File;
import java.io.FileNotFoundException;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/12/16.
 */

public class UploadFileService extends Service implements ICommonViewUi {
    public static final String COLLECT_FILE_PATH="path";
    public static final String COLLECT_FILE_TYPE="type";
    public static final String COLLECT_SENDER="sender";
    private ICommonRequestPresenter iCommonRequestPresenter;

    String url="",sender="";
    int type;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        iCommonRequestPresenter = new CommonRequestPresenterImpl(this, this);
        url=intent.getStringExtra(COLLECT_FILE_PATH);
        sender=intent.getStringExtra(COLLECT_SENDER);
        type=intent.getIntExtra(COLLECT_FILE_TYPE,0);//0 图片 1语音
        Log.e("url",url);
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
        return super.onStartCommand(intent, flags, startId);
    }




    @Override
    public void toRequest(int eventTag) {
        RequestParams map = new RequestParams();
//        map.put("accountName", InfoUtil.getLocalUserInfo(this).getAccountName());
        map.put("type", type);
        map.put("sendUser", sender);
        File file=new File(url);
        File[] files={file};
        try {
            map.put("files", files);
            map.setForceMultipartEntityContentType(true);
            iCommonRequestPresenter.request(ApiConstants.EventTags.BEGIN_EVENT, this, ApiConstants.Urls.ACCOUNT_COLLECT_ADD, map);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getRequestData(int eventTag, String result) {
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.MSG);
        postResult.setResult("Favorite success");
        EventBus.getDefault().post(postResult);
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
//        ToastUtil.showMessage(msg);
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.MSG);
        postResult.setResult(msg);
        EventBus.getDefault().post(postResult);

    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.MSG);
        postResult.setResult(msg);
        EventBus.getDefault().post(postResult);
//        ToastUtil.showMessage(msg);
    }
//    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        PostResult postResult = new PostResult();

        if(status){
            postResult.setTag(EventBusTags.SHOW_DIALOG);
//            mPostingdialog = new ECProgressDialog(this, "Waiting");
//            mPostingdialog.show();
        }else{
//            mPostingdialog.dismiss();
            postResult.setTag(EventBusTags.CANCEL_DIALOG);
        }
        EventBus.getDefault().post(postResult);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
